import { createRouter, createWebHistory } from 'vue-router'
import { useSSIDStore, usePortStore } from '@/stores/index'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [

    {
      path: '/',
      name: 'home',
      component: () => import('@/views/home/index.vue'),
      children: [
        {
          redirect: '/PocketAI',
          path: '/',
        },
        {
          path: '/PocketAI',
          name: 'PocketAI',
          component: () => import('@/views/PocketAI/index.vue'),
          children: [
            {
              path: '/PocketAI',
              redirect: '/PocketAI/knowledge',
            },
            {
              path: '/PocketAI/knowledge',
              name: 'knowledge',
              component: () => import('@/views/PocketAI/components/knowledge.vue')
            },
            {
              path: '/PocketAI/FileSAearch',
              name: 'FileSAearch',
              component: () => import('@/views/PocketAI/components/FileSAearch.vue')
            },
            {
              path: '/PocketAI/Blog',
              name: 'Blog',
              component: () => import('@/views/PocketAI/components/Blog.vue')
            },
            {
              path: '/PocketAI/PPT',
              name: 'PPT',
              component: () => import('@/views/PocketAI/components/PPT.vue')
            }
          ]
        },
        {
          path: '/WithNotes',
          name: 'WithNotes',
          component: () => import('@/views/WithNotes/index.vue')
        },
        {
          path: '/NASFiles',
          name: 'NASFiles',
          component: () => import('@/views/NASFiles/index.vue')
        },
        {
          path: '/album',
          name: 'album',
          component: () => import('@/views/album/index.vue')
        },
        {
          path: '/setUp',
          name: 'setUp',
          component: () => import('@/views/setUp/index.vue')
        },
      ]
    },
    {
      path: '/DeviceBinding',
      name: 'DeviceBinding',
      component: () => import('@/views/DeviceBinding/index.vue'),

    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login/index.vue'),

    },
    {
      path: '/allocation',
      name: 'allocation',
      component: () => import('@/views/allocation/index.vue'),

    }
  ]
})

const whitelist = ['allocation']
router.beforeEach((to, from, next) => {
  const { setPort, getPort, isInvalid } = usePortStore()
  if (to.path === '/login') {
    next()
  }
  if (to.path === '/allocation') {
    next()
  }
  if (to.path !== '/DeviceBinding'&& to.path !== '/login' && isInvalid()) {
    setPort({})
    next(`/DeviceBinding`)
  }
  if (to.path === '/DeviceBinding' && !isInvalid()) {
    next('/')
  }
  // if (whitelist.some(item => to.path.includes(item)) && !!useSSIDStore().ssid) {
  //   next('/')
  // }
  // if()
  next()
})

export default router
